package com.yimi.yinhepda.adapter.message;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.yimi.yinhepda.R;
import com.yimi.yinhepda.db.dao.UserReadMessageDao;
import com.yimi.yinhepda.utils.Common;
import com.pda.ym.utils.DateHelper;
import com.pda.ym.utils.SharedPreferUtil;
import com.yimi.yinhepda.entity.message.MyMessageBean;
import java.util.List;

/**
 * @author 000432
 * @date 2018/1/13 0013
 */
public class MessageListAdapter extends BaseAdapter {

  private Context mContext;
  private List<MyMessageBean> mLists;
  private UserReadMessageDao readMessageDao;

  public MessageListAdapter(Context context, List<MyMessageBean> lists) {
    this.mContext = context;
    this.mLists = lists;
    readMessageDao = new UserReadMessageDao();
  }

  @Override public int getCount() {
    return mLists.size();
  }

  @Override public Object getItem(int position) {
    return mLists.get(position);
  }

  @Override public long getItemId(int position) {
    return position;
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder holder;
    if (convertView == null) {
      holder = new ViewHolder();
      convertView = LayoutInflater.from(mContext).inflate(R.layout.item_message_list, null);
      holder.mTitle = (TextView) convertView.findViewById(R.id.myMsg_tv_title);
      holder.mPushTime = (TextView) convertView.findViewById(R.id.myMsg_tv_pushTime);
      holder.mContent = (TextView) convertView.findViewById(R.id.myMsg_tv_content);
      holder.mReadStatus = (ImageView) convertView.findViewById(R.id.myMsg_iv_readStatus);
      convertView.setTag(holder);
    } else {
      holder = (ViewHolder) convertView.getTag();
    }

    MyMessageBean bean = mLists.get(position);
    String titleStr = bean.getTitle();
    if (titleStr.length() > 10) {
      titleStr = titleStr.substring(0, 10) + "...";
    }
    holder.mTitle.setText(titleStr);
    holder.mPushTime.setText(DateHelper.DateToStr(DateHelper.StrToDate(bean.getPushTime(), DateHelper.timeFormat), DateHelper.timeFormat3));
    holder.mContent.setText(bean.getContent());

    String operator = SharedPreferUtil.getInstance().getValue(Common.USER_CODE);
    String notifyId = bean.getNotificationId();
    if (readMessageDao.exitReadMessage(operator, notifyId)) {
      holder.mReadStatus.setVisibility(View.INVISIBLE);
    } else {
      holder.mReadStatus.setVisibility(View.VISIBLE);
    }

    return convertView;
  }

  public static class ViewHolder {
    private TextView mTitle;
    private TextView mPushTime;
    private TextView mContent;
    private ImageView mReadStatus;
  }
}
